class DropList extends AWidget {

  ArrayList entries;

  // variables
  boolean opened = false;
  int start = 0, end = 33, x, LISTSIZEX, STARTPOSX;
  String name = "", chosen, all;
  PFont font;
  AWidgetListener listener = null;

  int chosenIdx = -1;
  
  // constructor
  public DropList(int a){

   if (a == 1){
      entries = dataBase.getUsers().values();
      chosen = "Users...";
      all = "All users";
      STARTPOSX = 70;
      LISTSIZEX = 160;
    }

    // tracks
    if (a == 3){
      entries = dataBase.getTracks().values();
      chosen = "Tracks...";
      all = "All tracks";
      STARTPOSX = 580;
      LISTSIZEX = 160;
    }

    // bands
    if (a == 4){
      entries = dataBase.getBands().values();
      chosen = "Bands...";
      all = "All bands";
      STARTPOSX = 330;
      LISTSIZEX = 160;
    }


      font = loadFont("LucidaSans-12.vlw");
      textFont(font, 12);

  }

  void draw(){
    // draw closed menu
    textAlign(LEFT);
    stroke(100);
    fill(#f2f2f2);
    rect(STARTPOSX,STARTPOSY,BUTTONSIZEX,BUTTONSIZEY);
    rect(STARTPOSX + BUTTONSIZEX,STARTPOSY,LISTSIZEX-BUTTONSIZEX, BUTTONSIZEY);
    fill(100);
    text(truncate(chosen,20), STARTPOSX+BUTTONSIZEX+2,STARTPOSY+12);
    text("v", STARTPOSX +4 ,23);

    if(mousePressed && mouseX > STARTPOSX && mouseX < STARTPOSX + BUTTONSIZEX && mouseY > STARTPOSY && mouseY < STARTPOSY + BUTTONSIZEY){  // if button is clicked
      opened = true;  // open menu
    }
    
    if(opened){  // if list is open
      fill(#f0f0f0);
      rect(STARTPOSX,STARTPOSY + BUTTONSIZEY,LISTSIZEX,LISTSIZEY);  // draw black rectangle - full menu
      fill(200);
      noStroke();
      if(mouseX > STARTPOSX && mouseX < STARTPOSX + LISTSIZEX && mouseY > STARTPOSY + BUTTONSIZEY && mouseY <= STARTPOSY + LISTSIZEY + BUTTONSIZEY - 2){  // if mouse is on list
       rect(STARTPOSX+1, 1 + mouseY - ((mouseY - (BUTTONSIZEY - STARTPOSY)) % 12), LISTSIZEX-2, 12);  // draw grey rectangle for selection
      }
      fill(0);
      fill(#c5c5c5);                                                                  
      text("^", STARTPOSX + LISTSIZEX/2 -3 ,STARTPOSY + BUTTONSIZEY + 12);    // draw up and down arrows
      text("v", STARTPOSX + LISTSIZEX/2 -3 ,STARTPOSY + LISTSIZEY + 12);      // i can use PNG's if someone wants to make some arrows with transparent backgrounds 
      
      stroke(255);

      x = 0;                                // counter for for loop
      for (int i = start; i < end; i++){    // for loop to select specified subset of the ArrayList
        if (i < entries.size()){            // if i < size of ArrayList
          name =getString(i);   // name = ArrayList.get(i)
          name = truncate(name,22);
          
        }
        else{
          name = "-";                       // otherwise name = "-" char
        }
        fill(150);                  
        text(name, STARTPOSX + 2, STARTPOSY+ BUTTONSIZEY + 24 + x * 12);  // print name at xpos, ypos + 2 rows + counter * row length
        x++;                                
      }

      if (mousePressed && mouseY > STARTPOSY + BUTTONSIZEY + 12 && mouseY <= STARTPOSY + LISTSIZEY + BUTTONSIZEY - 15 && mouseX >= STARTPOSX && mouseX < STARTPOSX + LISTSIZEX + BUTTONSIZEX){  // if mouse is on a value (i.e a user name)
        chosenIdx = ((mouseY - ((mouseY - STARTPOSY - 12 - BUTTONSIZEY) % 12)) /12)+start - 3;
        chosen = getString(chosenIdx);  // chosen = entries.get(index number)         
        if(listener != null){
          listener.valueChanged(this);
        }          
        this.close();  // close
      }
      else if (mousePressed && mouseY <= STARTPOSY + BUTTONSIZEY + 12  && mouseY >= STARTPOSY + BUTTONSIZEY && start > 0){ // if up button pressed
        start--;  // decrement values of subset
        end--;
      }
      else if (mousePressed && mouseY > STARTPOSY + LISTSIZEY + BUTTONSIZEY - 15  && end < entries.size()){  // if down button pressed
        start++;  // increment values of subset
        end++;
      }
      else if (mousePressed && mouseY <= STARTPOSY + BUTTONSIZEY + 24 && mouseY >= STARTPOSY + BUTTONSIZEY + 12){  // if all users/tracks etc button pressed
        chosen = all;
        this.close();
      }
      
    }
    if(mousePressed && (mouseX > STARTPOSX + LISTSIZEX || mouseX < STARTPOSX || mouseY > STARTPOSY + LISTSIZEY + BUTTONSIZEY || mouseY < STARTPOSY)){ // if mouse is off menu and pressed
      this.close();   // close menu
    }
  }

  void setListener(AWidgetListener listener){
    this.listener = listener;
  }
  


  String getString(int a){
    return entries.get(a).toString();
  }
  
  String getChosen(){
    return chosen;
  }
  
  Object getValue() {
    return entries.get(chosenIdx);
  }  
     
  void update(){
    entries = new ArrayList();
    
  }

  void close(){
    opened = false;  // know it's closed
  }
  
  boolean getFocus(){
    return !opened;
  }
  
  
 String truncate(String inputString,int amount){   //Really simple method that truncates a string if it is longer then "amount"
   String name = inputString;
   if(name.length() >= amount){
     name = name.substring(0, amount);
     name = name + "...";
   } 
   return(name);
  }
}


